package com.example.tvrecyclerviewkotlin

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.tvrecyclerviewkotlin.adapter.GameListAdapter
import com.example.tvrecyclerviewkotlin.adapter.MyRecyclerViewAdapter
import com.example.tvrecyclerviewkotlin.bean.GameListBean
import com.example.tvrecyclerviewkotlin.view.SpaceDecoration
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity(), MyRecyclerViewAdapter.OnItemClickListener,
    MyRecyclerViewAdapter.OnItemFocusChangeListener {
    private var adapter: MyRecyclerViewAdapter? = null

    //标题
    private val titles =
        arrayOf("首页", "游戏", "教育", "生活", "娱乐", "新闻", "直播", "我的")
    private var gameListAdapter: GameListAdapter? = null
    private var gameListBeans: MutableList<GameListBean>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initAdapter()
        initTimeAdapter()
        initListener()
    }

    /**
     * 初始化标题
     */
    private fun initAdapter() {
        adapter = MyRecyclerViewAdapter(this)
        rv_tab!!.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        rv_tab!!.adapter = adapter
        rv_tab!!.requestFocus()
        adapter!!.setData(titles)
        adapter!!.setOnFocusChangeListener(this)
        adapter!!.setOnItemClickListener(this)

    }

    /**
     * 初始化内容列表
     */
    private fun initTimeAdapter() {
        setData()
        gameListAdapter =
            object : GameListAdapter(gameListBeans, this, object : OnItemClickListener {
                override fun onItemClick(view: View?, position: Int) {}
            }) {
                override fun onItemFocus(itemView: View?) {
                    itemView?.isSelected = true
                    val view: View = itemView!!.findViewById(R.id.iv_bg)
                    view.isSelected = true
                }

                override fun onItemGetNormal(itemView: View?) {
                    itemView?.isSelected = true
                    val view: View = itemView!!.findViewById(R.id.iv_bg)
                    view.isSelected = true
                }
            }

        rv_time_list.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        rv_time_list.addItemDecoration(SpaceDecoration(30))
        rv_time_list.adapter = gameListAdapter
    }

    private fun initListener() {

    }

    private fun initView() {
    }


    override fun onItemClick(view: View?, position: Int) {
        updateData(titles[position])
    }

    /**
     * 初始化数据
     */
    private fun setData() {
        gameListBeans = ArrayList()
        for (i in 0..14) {
            val gameListBean = GameListBean("2", "王者荣耀")
            (gameListBeans as ArrayList<GameListBean>).add(gameListBean)
        }
    }

    /**
     * 更新数据，这里是模拟数据，真实的项目是请求接口刷新数据
     */
    private fun updateData(title: String) {
        if (gameListBeans != null && gameListBeans!!.size > 0) {
            gameListBeans!!.clear()
        }
        for (i in 0..14) {
            val gameListBean = GameListBean("1", title)
            gameListBeans!!.add(gameListBean)
        }
        gameListAdapter!!.notifyDataSetChanged()
    }


    override fun onItemFocusChange(view: View?, position: Int, hasFocus: Boolean) {}
}